package com.time.company.components.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.time.company.R;
import com.time.company.base.BaseActivity;
import com.time.company.utils.ToastUtils;

/**
 * Created by Bing on 2017/11/27.
 * E-mail is tx16bing@163.com
 */

public class ForgetPsdActivity extends BaseActivity {

    private static final String EXTRA_FLAG = "extra_flag";
    private static final int FORGET_PSD_NEXT_REQUEST_CODE = 11;

    private EditText etTelephone, etPsd, etAgainPsd;
    private Button btnNext;

    public static void start(Activity activity, String flag) {
        Intent intent = new Intent(activity, ForgetPsdActivity.class);
        intent.putExtra(EXTRA_FLAG, flag);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_psd, getString(R.string.activity_forget_title));
        assignViews();
    }

    private void assignViews() {
        etTelephone = findView(R.id.et_forget_bind_telephone);
        etPsd = findView(R.id.et_forget_bind_psd);
        etAgainPsd = findView(R.id.et_forget_bind_psd_again);
        btnNext = findView(R.id.btn_forget_next);
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!checkForm()) {
                    return;
                }

                ForgetPsdNextActivity.start(
                        ForgetPsdActivity.this,
                        FORGET_PSD_NEXT_REQUEST_CODE,
                        etTelephone.getText().toString().trim(),
                        etPsd.getText().toString().trim(),
                        getIntent().getStringExtra(EXTRA_FLAG));
            }
        });
    }

    private boolean checkForm() {
        boolean isFormValid = true;
        if (TextUtils.isEmpty(etTelephone.getText().toString().trim())) {
            isFormValid = false;
            ToastUtils.showShort(getString(R.string.please_input_telephone));
        } else if (etTelephone.getText().toString().trim().length() < 11) {
            isFormValid = false;
            ToastUtils.showShort(getString(R.string.please_input_legal_telephone));
        } else if (TextUtils.isEmpty(etPsd.getText().toString().trim())) {
            isFormValid = false;
            ToastUtils.showShort(getString(R.string.please_input_psd));
        } else if (TextUtils.isEmpty(etAgainPsd.getText().toString().trim())) {
            isFormValid = false;
            ToastUtils.showShort(getString(R.string.please_input_psd_again));
        }else if (etPsd.getText().toString().trim().length() < 6 || etAgainPsd.getText().toString().trim().length() < 6) {
            isFormValid = false;
            ToastUtils.showShort(getString(R.string.please_input_legal_psd));
        }

        return isFormValid;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) return;
        switch (requestCode){

            case FORGET_PSD_NEXT_REQUEST_CODE:
                finish();
                break;

            default:
                break;
        }
    }
}
